show_notice();

getSliders();

scorllInit();

showGdsNum();

var goods_category_id = 1;
var cart = {};
//分页参数设置
fenye_options.url = index_goods_list;
fenye_options.dataName = 'goods_list';
fenye_options.params = {category_id:goods_category_id};
fenye_options.callback = function(res){
    console.log(res)
    //先解绑事件，防止上拉加载造成二次绑定，导致事件被执行多次
    $(".cacu-btn").unbind('click');
    //绑定点击事件，添加购物车
    $(".cacu-btn").bind('click',function(){
        var self = $(this);
        var parent = self.parent().parent();
        var goods_id = parseInt(self.attr('data'));
        var goodsData =
        {
            'id': goods_id,
            'goods_img': parent.find('img').attr('src'),
            'goods_name': parent.find('.goods-title').text(),
            'goods_price': parent.find('.goods-price').attr('data'),
            'goods_num': 1,
            "selected": true,
        }
        addCart(goodsData);
    });
};

//选项卡切换下拉加载
mui('#segmentedControl').on('tap','a',function(e){
    window.top.location.href=this.href;
    var gc_id = parseInt(this.getAttribute('data'));
    if (goods_category_id == gc_id) {
        return;
    }
    goods_category_id = gc_id;
    page = 1;
    is_more = true;
    fenye_options.params = {category_id:goods_category_id}
    $('#fenye_list').empty();
    mui("#scroll").pullRefresh().enablePullupToRefresh(); //启用上拉加载控件
    mui('#scroll').pullRefresh().refresh(true); //重置上拉
    mui('#scroll').pullRefresh().endPullupToRefresh(false);//开启加载
    mui('#scroll').pullRefresh().scrollTo(0, 0, 100); //滚动到顶部
});

function scorllInit() {
    var info = getScreen();
    var slider_height = parseInt($(".mui-slider").height());
    var category_height = parseInt($("#segmentedControl").height());
    var bar_height = parseInt($(".mui-bar").height());
    var scroll_height = info[1] - slider_height - category_height - bar_height + 'px';
    fenye_options.scrollHeight = scroll_height; //设置分页滚轴高度（自适应）
    mui('.mui-control-content').each(function() {
        this.style.height = scroll_height;
    });
    mui('.mui-scroll-wrapper').each(function(k, v) {
        mui(v).scroll({
            deceleration: (mui.os.ios ? 0.003 : 0.0006),
            indicators: false  //隐藏一条滚动条 增大减速系数。。。
        });
    });
}

function show_notice() {
    var success = function (res) {
        var msg = '';
        var status = 0;
        if (typeof(res.data) == 'object') {
            var flag = false;
            for (var i in res.data) {
                flag = true;
                break;
            }
            if (flag) {
                msg = res.data.msg;
                status = res.data.status;
            }
        }
        var html = template('notice_data', [status,msg]);     //order 模板id
        $('#notice').append(html);
    };
    getData(shop_notice, comParams(), success);
}

// 轮播图
function getSliders() {
    var success = function (res) {
        var data = [];
        if (res.data.shop_list.length > 0) {
            data = res.data.shop_list[0];
            // console.log(data)
        }
        template.defaults.imports.slider_nums = data.sliders.length - 1;
        var html = template('slider_list_data', data);     //order 模板id
        $('#sliders').append(html);
        var gallery = mui('.mui-slider');
        gallery.slider({
            interval:3000//自动轮播周期，若为0则不自动播放，默认为0；
        });
        //计算轮播图片的高度
        //1:获取页面的宽度
        var window_width = document.body.clientWidth;
        console.log(window_width);
        var height = Math.floor(window_width/2.8 * 100) / 100;
        console.log(height);
        $('.img_slider').attr('height',height); //给图片赋高
        $('.mui-slider').css('height',height);//给轮播主盒子副高
        
        $('.mui-control-item').bind('click',function () {

        })
    };
    var member_info = getCache(login_key);
    getData(shop_index, combineParams({aid:member_info.aid}), success);
}


